package com.ccdc.lbcmsystem.controller;


import com.ccdc.lbcmsystem.mapper.ChildrenMapper;
import com.ccdc.lbcmsystem.mapper.ElderlyMapper;
import com.ccdc.lbcmsystem.mapper.ExamineMapper;
import com.ccdc.lbcmsystem.mapper.UserMapper;
import com.ccdc.lbcmsystem.pojo.*;
import com.ccdc.lbcmsystem.util.SMSSentUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ElderlyController {

    @Autowired
    ElderlyMapper elderlyMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ExamineMapper examineMapper;

    @RequestMapping("/elderlySMSSent/{id}")
    public String SMSSent(@PathVariable("id") Integer id) throws Exception {
        Elderly elderly = elderlyMapper.queryElderlyById(id);
        String guardianPhone = elderly.getGuardianPhone();
        System.out.println(guardianPhone);
        String host = "http://api.guoyangyun.com";
        String path = "/api/sms/smsoto.htm";
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appkey", "1725670970202304031648128407995");
        paramMap.put("appsecret", "f421d50df5f508fd025bbd0185ef8e3c");
        paramMap.put("smsSignId", "cd4ab2b471f64a40be51a0b4f2e429e5"); // 签名ID
        paramMap.put("templateId", "32dd138ca0634a6289b7988a547aa57b"); // 模板ID
        paramMap.put("mobile",guardianPhone); // 手机号码
        try{
            HttpResponse httpResponse = SMSSentUtil.doGet(host, path, paramMap);
            System.out.println(httpResponse);
            // response body
            System.out.println(EntityUtils.toString(httpResponse.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/elderlyList";
    }

    @RequestMapping("/elderlyList")
    public String elderlyList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                               @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Elderly> elderlies = elderlyMapper.queryElderlyList();
        PageInfo<Elderly> elderliesPageInfo = new PageInfo<>(elderlies);
        model.addAttribute("elderliesPageInfo", elderliesPageInfo);
        model.addAttribute("elderlies",elderlies);
        return "elderly/elderlyList";
    }

    @RequestMapping("/searchElderly")
    public String searchElderly(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Elderly> elderlies = elderlyMapper.queryListElderlyByName(name);
        PageInfo<Elderly> elderliesPageInfo = new PageInfo<>(elderlies);
        session.setAttribute("name",name);
        model.addAttribute("elderliesPageInfo", elderliesPageInfo);
        model.addAttribute("elderlies",elderlies);
        return "elderly/elderlySearchList";
    }

    @RequestMapping("/toElderlyAdd")
    public String toElderlyAdd(){
        return "elderly/elderlyAdd";
    }

    @RequestMapping("/elderlyAdd")
    @ResponseBody
    public String elderlyAdd(Elderly elderly) {
        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        elderly.setCreateTime(time);
        elderly.setDeleteStatus(0);
        try {
            elderlyMapper.addElderly(elderly);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }

    @RequestMapping("/elderlyLookInfo/{id}")
    public String elderlyLookInfo(@PathVariable("id") Integer id, Model model){
        Elderly elderly = elderlyMapper.queryElderlyById(id);
        model.addAttribute("elderly",elderly);
        return "elderly/elderlyLookInfo";
    }

    @RequestMapping("/toElderlyEdit/{id}")
    public String toChildrenEdit(@PathVariable("id") Integer id, Model model){
        Elderly elderly = elderlyMapper.queryElderlyById(id);
        model.addAttribute("elderly",elderly);
        return "elderly/elderlyEdit";
    }


    @RequestMapping("/elderlyEdit")
    @ResponseBody
    public String elderlyEdit(String name,Integer id,Integer age,String phone,String birthday,String address,
                               String guardian,String guardianPhone,String healthCondition,Integer health
                                ,Integer familyStatus){
        String msg = "";
        try{
            elderlyMapper.updateElderlyById(id,name, phone,birthday,age, address
                    ,guardian, guardianPhone,healthCondition, health,familyStatus);
            msg = "OK";
            return msg;
        }catch (Exception e){
            System.out.println(e);
            msg = "修改失败";
            return msg;
        }

    }

    @RequestMapping("/elderlyDelete")
    @ResponseBody
    public String elderlyDelete(Integer id, Model model, String examineUser, Delelderly delelderly, String reason){
        String msg = "";

        System.out.println(examineUser);
        try {
            User user = userMapper.queryUserByUserName(examineUser);
            Elderly elderly = elderlyMapper.queryElderlyById(id);
            System.out.println(user);
            //工作人员删除 需要提交给管理员审核
            if (user.getRole().equals("6")){
                Integer deleteStatus = 2;//将删除状态码修改成 2   提交给管理员审核
                elderlyMapper.delElderlyById(id,deleteStatus);
                Delelderly delelderly1 = examineMapper.queryDelElderly(id);
                if (delelderly1 != null){
                    msg = "该儿童信息已提交删除申请,请勿重复操作!";
                    return msg;
                }else {
                    delelderly.setDelUser(examineUser);
                    delelderly.setId(elderly.getId());
                    delelderly.setName(elderly.getName());
                    delelderly.setPhone(elderly.getPhone());
                    delelderly.setBirthday(elderly.getBirthday());
                    delelderly.setAge(elderly.getAge());
                    delelderly.setAddress(elderly.getAddress());
                    delelderly.setGuardian(elderly.getGuardian());
                    delelderly.setGuardianPhone(elderly.getGuardianPhone());
                    delelderly.setHealth(elderly.getHealth());
                    delelderly.setHealthCondition(elderly.getHealthCondition());
                    delelderly.setFamilyStatus(elderly.getFamilyStatus());
                    delelderly.setDeleteStatus(2);
                    delelderly.setReason(reason);
                    SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date createTime = new Date();
                    String time = sft.format(createTime);
                    delelderly.setCreateTime(time);
                    examineMapper.addDelElderly(delelderly);
                    msg = "OK";
                    return msg;
                }
            }else {
                Integer deleteStatus = 1;
                elderlyMapper.delElderlyById(id,deleteStatus);
                msg = "OK";
                return msg;
            }

        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }
}
